/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ShapeManagement.bounceboxapp;

import ShapeManagement.bouncebox.BounceBox;
import ShapeManagement.bouncebox.Circle;
import ShapeManagement.bouncebox.Square;
import ShapeManagement.bouncebox.Rectangle;
import ShapeManagement.bouncebox.Triangle;
import java.awt.Color;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 *
 * @author ashongtical
 */



public class Main {

    public static void main(String[] args) throws FileNotFoundException {
        setUpDemo1();
    }

    public static void setUpDemo1() throws FileNotFoundException {
        int circleCount = 0;
        int squareCount = 0;
        int rectangleCount = 0;
        int triangleCount = 0;
        double totalArea = 0.0;
        BounceBox box = new BounceBox(700, 500);
        FileInputStream fileIn = new FileInputStream("practical4.txt");
        Scanner scanner = new Scanner(fileIn);
        PrintWriter fileOut = new PrintWriter("shapesArea.txt");

        while (scanner.hasNext()) {
            String type = scanner.next();

            switch (type) {
                case "Circle":
                    double s1 = handleCircle(box, scanner);
                    totalArea += s1;
                    circleCount++;
                    break;

                case "Square":
                    double s2 = handleSquare(box, scanner);
                    totalArea += s2;
                    squareCount++;
                    break;

                case "Triangle":
                    double s3 = handleTriangle(box, scanner);
                    totalArea += s3;
                    triangleCount++;
                    break;

                case "Rectangle":
                    double s4 = handleRectangle(box, scanner);
                    totalArea += s4;
                    rectangleCount++;
                    break;

                default:
                    System.out.println("\u001B[31m" + type + " is not a recognized shape\u001B[0m");
                    scanner.nextLine();
            }
        }

        System.out.println("Circle: " + circleCount + "\tSquare: " + squareCount);
        System.out.println("Triangle: " + triangleCount + "\tRectangle: " + rectangleCount);
        System.out.println("Total Area: " + totalArea);

        fileOut.println("Circle: " + circleCount + "\tSquare: " + squareCount);
        fileOut.println("Triangle: " + triangleCount + "\tRectangle: " + rectangleCount);
        fileOut.println("Total Area: " + totalArea);

        box.start();
        scanner.close();
        fileOut.close();
    }

    private static double handleCircle(BounceBox box, Scanner scanner) {
        int x = scanner.nextInt();
        int y = scanner.nextInt();
        int radius = scanner.nextInt();

        Circle circle = new Circle(x, y, radius);

        if (scanner.hasNextInt()) {
            int vx = scanner.nextInt();
            int vy = scanner.nextInt();
            circle.setVelocity(vx, vy);

            if (scanner.hasNextInt()) {
                int r = scanner.nextInt();
                int g = scanner.nextInt();
                int b = scanner.nextInt();
                circle.setColor(new Color(r, g, b));
            } else {
                circle.setColor(Color.BLACK);
            }
        } else {
            if (scanner.hasNextInt()) {
                int r = scanner.nextInt();
                int g = scanner.nextInt();
                int b = scanner.nextInt();
                circle.setColor(new Color(r, g, b));
            } else {
                circle.setColor(Color.BLACK);
            }
        }

        box.addShape(circle);
        return Math.PI * radius * radius;
    }

    private static double handleSquare(BounceBox box, Scanner scanner) {
        int x = scanner.nextInt();
        int y = scanner.nextInt();
        int width = scanner.nextInt();

        Square square = new Square(x, y, width);

        if (scanner.hasNextInt()) {
            int vx = scanner.nextInt();
            int vy = scanner.nextInt();
            square.setVelocity(vx, vy);

            if (scanner.hasNextInt()) {
                int r = scanner.nextInt();
                int g = scanner.nextInt();
                int b = scanner.nextInt();
                square.setColor(new Color(r, g, b));
            } else {
                square.setColor(Color.BLACK);
            }
        } else {
            if (scanner.hasNextInt()) {
                int r = scanner.nextInt();
                int g = scanner.nextInt();
                int b = scanner.nextInt();
                square.setColor(new Color(r, g, b));
            } else {
                square.setColor(Color.BLACK);
            }
        }

        box.addShape(square);
        return width * width;
    }

    private static double handleTriangle(BounceBox box, Scanner scanner) {
        int x = scanner.nextInt();
        int y = scanner.nextInt();
        int base = scanner.nextInt();
        int height = scanner.nextInt();

        Triangle triangle = new Triangle(x, y, base, height);

        if (scanner.hasNextInt()) {
            int vx = scanner.nextInt();
            int vy = scanner.nextInt();
            triangle.setVelocity(vx, vy);

            if (scanner.hasNextInt()) {
                int r = scanner.nextInt();
                int g = scanner.nextInt();
                int b = scanner.nextInt();
                triangle.setColor(new Color(r, g, b));
            } else {
                triangle.setColor(Color.BLACK);
            }
        } else {
            if (scanner.hasNextInt()) {
                int r = scanner.nextInt();
                int g = scanner.nextInt();
                int b = scanner.nextInt();
                triangle.setColor(new Color(r, g, b));
            } else {
                triangle.setColor(Color.BLACK);
            }
        }

        box.addShape(triangle);
        return 0.5 * base * height;
    }

    private static double handleRectangle(BounceBox box, Scanner scanner) {
        int x = scanner.nextInt();
        int y = scanner.nextInt();
        int width = scanner.nextInt();
        int height = scanner.nextInt();

        Rectangle rectangle = new Rectangle(x, y, width, height);

        if (scanner.hasNextInt()) {
            int vx = scanner.nextInt();
            int vy = scanner.nextInt();
            rectangle.setVelocity(vx, vy);

            if (scanner.hasNextInt()) {
                int r = scanner.nextInt();
                int g = scanner.nextInt();
                int b = scanner.nextInt();
                rectangle.setColor(new Color(r, g, b));
            } else {
                rectangle.setColor(Color.BLACK);
            }
        } else {
            if (scanner.hasNextInt()) {
                int r = scanner.nextInt();
                int g = scanner.nextInt();
                int b = scanner.nextInt();
                rectangle.setColor(new Color(r, g, b));
            } else {
                rectangle.setColor(Color.BLACK);
            }
        }

        box.addShape(rectangle);
        return width * height;
    }
}

